package rs.pnv.pcc;

import javax.jws.HandlerChain;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import rs.pnv.model.BankPartner;
import rs.pnv.model.CardAuthenticationRequest;
import rs.pnv.model.PCCResponse;
import rs.pnv.service.BankPartnerService;

@WebService(endpointInterface = "rs.pnv.pcc.Pcc")
@HandlerChain(file = "/handler-chain.xml")
public class PccImpl implements Pcc {
	
	@Autowired
	BankPartnerService bankPartnerService;

	@Override
	public PCCResponse processAcquirerAut(CardAuthenticationRequest cardAuthReq) {
		
		System.out.println("PccImpl.processAcquirerAut()");
		
		
		String issuerId = cardAuthReq.getPan().substring(0, 6);
		BankPartner issuerBank = null;
		System.out.println("Issuer id: " + issuerId);
		try {
			issuerBank = bankPartnerService.findByBankId(issuerId);
		} catch (NullPointerException e) {
			System.out.println("\n***********null pointer exc : Nepostojeca banka*************\n");
			//e.printStackTrace();
		}
		
		System.out.println("test search: " + bankPartnerService.findByBankName("Ime1").getWsdlLocation());
		
		/*
		URL wsdlLocation = null;
		try {
			wsdlLocation = new URL(issuerBank.getWsdlLocation());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		QName serviceName = new QName(issuerBank.getDefaultNamespace(), issuerBank.getServiceNAme());
		Service service = Service.create(wsdlLocation, serviceName);
		*/
		
		/*
		Bank bank = service.getPort(Bank.class);
		IssuerResponse response = bank.processAcquirerAut(acquirer, card, amount);
		
		BankImplServiceLocator bisl = new BankImplServiceLocator(issuerBank.getWsdlLocation(), new QName(issuerBank.getDefaultNamespace(), issuerBank.getDefaultNamespace()));
		Bank bank = bisl.getBankImplPort();

		rs.pnv.bank.IssuerResponse response = bank.processAcquirerAut(new rs.pnv.bank.BankAut(), new rs.pnv.bank.CardInformation(), new BigDecimal(50));

		IssuerResponse iResponse = new IssuerResponse();
		iResponse.setAcquirerAut(acquirerAut);
		iResponse.setResult(response.getResult());

		return response;
		*/
		
		PCCResponse resp = new PCCResponse();
		//resp.setCertificateLocation(issuerBank.getCertificateLocation());
		return resp;


	}

}
